package com.qc31.baselib.utils

import com.google.gson.Gson
import com.google.gson.JsonParser

/**
 *@Author Jasper
 *@Time   2020/7/21
 *@Desc   todo
 */
object GsonUtil {
    val gson = Gson()

    inline fun <reified T> formObject(json: String?): T {
        return gson.fromJson(json, T::class.java)
    }

    fun <T> formArrayList(json: String?, clazz: Class<T>): ArrayList<T> {
        val asJsonArray = JsonParser.parseString(json).asJsonArray
        val list = ArrayList<T>()
        for (jsonElement in asJsonArray) {
            list.add(gson.fromJson(jsonElement, clazz))
        }
        return list
    }

    fun <T> toJson(t: T): String {
        return gson.toJson(t)
    }

}

